﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QunYing.BLL;
using QunYing.Common;
using Spore.Validator;
using Image = System.Drawing.Image;

namespace Client
{
    public partial class FrmAddActivity : BasePopForm
    {
        QunYing.Common.IImageOperator opera = QunYing.Common.ClientProxyFactory.GetProxy<QunYing.Common.IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationGoods);
        public static FrmAddActivity frm = null;
        public QunYing.Model.ActivityGoods editModel = null;
        public string OPERTYPE = "ADD";
        private string strPath = string.Empty;
        public FrmAddActivity()
        {
            InitializeComponent();
            this.NecessaryRemark(this.btnSel, this.txtImage);
            this.FormValidator.SetControlValitors(this.btnSel, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txtImage, ValidatorDefaults.NotNull);
            spanDate.StartDateEdit.DateTime = DateTime.Now.Date;
            frm = this;
        }

        private void FrmAddActivity_Load(object sender, EventArgs e)
        {
            if (OPERTYPE == "EDIT" && editModel != null)
            {
                BindInfo(editModel);
            }
        }


        private void btnSel_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ShowFrm();
        }

        private void ShowFrm()
        {
            FrmSelGoods frm = new FrmSelGoods();
            frm.str = btnSel.Text;
            frm.ShowDialog();
        }

        private void btnSel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                ShowFrm();
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            OpenFileDialog dag = new OpenFileDialog();
            dag.Filter = "Image Files(*.JPG;*.GIF;*.PNG;*.BMP)|*.JPG;*.GIF;*PNG,*.BMP";
            if (dag.ShowDialog() == DialogResult.OK)
            {
                Image image = Image.FromFile(dag.FileName);
                pictureEdit1.Image = image;
                txtImage.Text = dag.FileName;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            pictureEdit1.Image = null;
            txtImage.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!this.FormValidator.ValidAll()) return;
            if (btnSel.Properties.Tag == null)
            {
                GlobalInfo.MsgBoxShowError("请选择活动商品！");
                return;
            }
            if (txtImage.Text == "")
            {
                GlobalInfo.MsgBoxShowError("请选择宣传图片！");
                return;
            }
            if (spanDate.StartDateEdit.Text == "")
            {
                GlobalInfo.MsgBoxShowError("请选择活动开始时间！");
                return;
            }
            strPath = GetImagePath();
            QunYing.Model.ActivityGoods model = new QunYing.Model.ActivityGoods();
            model.经销商编号 = GlobalInfo.UserLoginInfo.用户ID;
            model.商品ID = Convert.ToInt32(btnSel.Properties.Tag);
            model.活动内容 = txtContent.Text.Trim();
            model.开始时间 = spanDate.StartDateEdit.DateTime.Date;

            if (spanDate.EndDateEdit.Text != "")
            {
                model.结束时间 = spanDate.EndDateEdit.DateTime.Date.AddHours(23).AddMinutes(59).AddSeconds(59);
            }
            QunYing.BLL.ActivityGoods activityGoods = new ActivityGoods();
            if (OPERTYPE == "ADD")
            {
                model.宣传图片 = QunYing.Common.GlobalInfo.ImageRelativePath + strPath;
                model.显示顺序 = 1;
                int k = activityGoods.Add(model);
                if (k > 0)
                {
                    UploadImg();
                    GlobalInfo.MsgBoxShow("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FrmActivityGoods.frm.QueryData();
                    this.Close();
                }
                else
                {
                    GlobalInfo.MsgBoxShowError("添加失败!");
                }
            }
            else if (OPERTYPE == "EDIT")
            {
                model.活动ID = editModel.活动ID;
                model.宣传图片 = editModel.宣传图片;
                model.显示顺序 = editModel.显示顺序;
                bool b = activityGoods.Update(model);
                if (b)
                {
                    EditImg(model.宣传图片);
                    GlobalInfo.MsgBoxShow("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FrmActivityGoods.frm.QueryData();
                    this.Close();
                }
                else
                {
                    GlobalInfo.MsgBoxShowError("修改失败!");
                }
            }

        }
        private bool UploadImg()
        {
            try
            {
                
                opera.UpLoad("", new string[] { strPath }, new Bitmap[] { (Bitmap)Bitmap.FromFile(txtImage.Text) });
            }
            catch (Exception ex)
            {
                QunYing.Common.Loger.Log.Error("上传图片时发生错误:" + ex.Message);
            }
            return true;
        }
        private bool EditImg(string path)
        {
            try
            {
                if (txtImage.Text.StartsWith(QunYing.Common.GlobalInfo.ImageRelativePath))
                {
                    return false;
                }
                opera.UpLoad("", new string[] { path.Substring(9)}, new Bitmap[] { (Bitmap)Bitmap.FromFile(txtImage.Text) });
            }
            catch (Exception ex)
            {
                QunYing.Common.Loger.Log.Error("上传图片时发生错误:" + ex.Message);
            }
            return true;
        }

        private string GetImagePath()
        {
            if (string.IsNullOrEmpty(txtImage.Text))
            {
                return "";
            }
            string strType = txtImage.Text.Substring(txtImage.Text.LastIndexOf('.'));
            string strPath = @"Activity\" + GlobalInfo.UserLoginInfo.用户ID + @"\" + (new QunYing.BLL.ActivityGoods().GetMaxId()) + strType;
            return strPath;
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            QunYing.Common.ClientProxyFactory.Close(opera);
            base.OnFormClosed(e);
        }

        private void BindInfo(QunYing.Model.ActivityGoods model)
        {
            btnSel.Text = new QunYing.BLL.GoodsInfo().GetModel(model.商品ID).商品名称;
            btnSel.Properties.Tag = model.商品ID.ToString();
            txtContent.Text = model.活动内容;
            txtImage.Text = model.宣传图片;
            if (model.开始时间 != null)
            {
                spanDate.StartDateEdit.DateTime = Convert.ToDateTime(model.开始时间);
            }
            else
            {
                spanDate.StartDateEdit.Text = "";
            }
            if (model.结束时间 != null)
            {
                spanDate.EndDateEdit.DateTime = Convert.ToDateTime(model.结束时间);
            }
            else
            {
                spanDate.EndDateEdit.Text = "";
            }

            Bitmap[] bp = opera.GetImgs(new string[] { model.宣传图片 });

            if (bp.Length != 0)
            {
                this.pictureEdit1.Image = (Image)bp[0].Clone();
            }
        }
    }
}